package m.jclecai.com.view.klpk;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.FrameLayout;

/**
 * Created by andy on 2017/6/14.
 */

public class CheckedFrameLayout extends FrameLayout implements Checkable {

    boolean isChecked=false;

    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};


    public CheckedFrameLayout(Context context) {
        this(context,null);
    }

    public CheckedFrameLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CheckedFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        int[] drawableState = super.onCreateDrawableState(extraSpace + 1);

        if(isChecked){
            mergeDrawableStates(drawableState,CHECKED_STATE_SET);
        }

        return drawableState;
    }


    @Override
    public void setChecked(boolean checked) {
        if (isChecked!=checked) {
            isChecked=checked;
            refreshDrawableState();
        }
    }

    @Override
    public boolean isChecked() {
        return isChecked;
    }

    @Override
    public void toggle() {
        setChecked(!isChecked);
    }
}
